package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.AuditNodeMapper;
import com.ruoyi.system.domain.AuditNode;
import com.ruoyi.system.service.IAuditNodeService;

/**
 * 审批流配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-07
 */
@Service
public class AuditNodeServiceImpl extends ServiceImpl<AuditNodeMapper, AuditNode> implements IAuditNodeService
{
    @Autowired
    private AuditNodeMapper auditNodeMapper;
    
    @Autowired
    private IAuditNodeService auditNodeService;

    /**
     * 查询审批流配置
     * 
     * @param auditNodeId 审批流配置主键
     * @return 审批流配置
     */
    @Override
    public AuditNode selectAuditNodeByAuditNodeId(Long auditNodeId)
    {
        return auditNodeMapper.selectAuditNodeByAuditNodeId(auditNodeId);
    }

    /**
     * 查询审批流配置列表
     * 
     * @param auditNode 审批流配置
     * @return 审批流配置
     */
    @Override
    public List<AuditNode> selectAuditNodeList(AuditNode auditNode)
    {
        return auditNodeMapper.selectAuditNodeList(auditNode);
    }

    /**
     * 新增审批流配置
     * 
     * @param auditNode 审批流配置
     * @return 结果
     */
    @Override
    public int insertAuditNode(AuditNode auditNode)
    {
        auditNode.setCreateBy(SecurityUtils.getUsername());
        auditNode.setCreateTime(DateUtils.getNowDate());
        return auditNodeMapper.insertAuditNode(auditNode);
    }

    @Override
    @Transactional
    public int saveAuditNode(List<AuditNode> auditNodes) {
        // 取第一条数据的auditBusinessType
        String auditBusinessType = auditNodes.get(0).getAuditBusinessType();

        // 删除该auditBusinessType的数据
        auditNodeMapper.deleteAuditNodeByAuditBusinessType(auditBusinessType);
        String username = SecurityUtils.getUsername();
        auditNodes.forEach(auditNode -> {
            auditNode.setAuditNodeId(null); // 设置主键为null，让数据库自动生成
            auditNode.setCreateBy(username);
            auditNode.setCreateTime(DateUtils.getNowDate());
        });

        // 批量插入新提交的数据
        this.saveBatch(auditNodes);
        return 1;
    }

    /**
     * 修改审批流配置
     * 
     * @param auditNode 审批流配置
     * @return 结果
     */
    @Override
    public int updateAuditNode(AuditNode auditNode)
    {
        auditNode.setUpdateTime(DateUtils.getNowDate());
        return auditNodeMapper.updateAuditNode(auditNode);
    }

    /**
     * 批量删除审批流配置
     * 
     * @param auditNodeIds 需要删除的审批流配置主键
     * @return 结果
     */
    @Override
    public int deleteAuditNodeByAuditNodeIds(Long[] auditNodeIds)
    {
        return auditNodeMapper.deleteAuditNodeByAuditNodeIds(auditNodeIds);
    }

    /**
     * 删除审批流配置信息
     * 
     * @param auditNodeId 审批流配置主键
     * @return 结果
     */
    @Override
    public int deleteAuditNodeByAuditNodeId(Long auditNodeId)
    {
        return auditNodeMapper.deleteAuditNodeByAuditNodeId(auditNodeId);
    }

    @Override
    public List<AuditNode> noPageList(String auditBusinessType) {
        AuditNode auditNode = new AuditNode();
        auditNode.setAuditBusinessType(auditBusinessType);
        return auditNodeMapper.selectAuditNodeList(auditNode);
    }
}